﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ufida.u9.ait.openapi.utils;
using UFIDA.U9.CBO.MFG.WorkCenter;
using UFIDA.U9.MO.MO;
using UFIDA.U9.MO.SFC.DispatchOrder;
using UFSoft.UBF.Business;

namespace ufida.u9.ait.openapi
{
    internal class DispatchOrderBase
    {
        public static DispatchOrderDoc ApproveDoc(DispatchOrderDoc doc)
        {
            ApproveDispatchOrderBP bp = new ApproveDispatchOrderBP();
            bp.CurrentSysVersion = doc.SysVersion;
            bp.DispatchOrderKey = doc.Key;
            bp.Do();

            return doc;
        }

        public static DispatchOrderDoc CreateDoc(DispatchOrderAITDTOData dto)
        {
            DispatchOrderDoc doc;
            using (ISession session = Session.Open())
            {
                doc = DispatchOrderDoc.Create();
                CreateHead(doc, dto);
                CreateLine(doc, dto);

                session.Commit();
            }

            return doc;

        }

        private static void CreateLine(DispatchOrderDoc doc, DispatchOrderAITDTOData dto)
        {
            //缓存MO工序
            Dictionary<String, MOOperation> operdic = new Dictionary<String, MOOperation>();
            foreach (var operline in doc.MO.MOOperations)
            {
                operdic.Add(operline.OperationNum, operline);
            }
            //新增行
            foreach (var dtoline in dto.DispatchOrderDocLines)
            {
                DispatchOrderDocLine line = DispatchOrderDocLine.Create(doc);
                line.LineNum = dtoline.LineNum;
                line.MO = doc.MO;
                line.MOOperation = operdic[dtoline.MOOperation];
                if (line.MOOperation == null)
                    throw new Exception($"订单{dto.MO.Code},工序号{dtoline.MOOperation}不存在!");

                //工作中心
                if (doc.WorkCenter != null)
                {
                    line.WorkCenter = doc.WorkCenter;
                }
                else
                {
                    line.WorkCenter = line.MOOperation.WorkCenter;
                }
                
                line.TaskCode = line.MOOperation.TaskCode;
                line.Task = line.MOOperation.StdOp;
                line.ProductUOM = line.MOOperation.OutputUOM;
                line.PlanStartDate = doc.PlanStartDate;
                line.PlanEndDate = doc.PlanCompleteDate;
                line.OpDescription = line.MOOperation.OpDescription;
                line.LineState = UFIDA.U9.MO.Enums.DPLineStateEnum.NotDisPatch;
                line.ItemMaster = doc.ItemMaster;
                line.DispatchUOM = line.ProductUOM;
                line.DispatchQty = dtoline.DispatchQty;

                line.ActualPrepareHours = dtoline.RealLaborPrepareHours;//实际准备工时
                line.ActualProcessHours= dtoline.RealLaborProcessHours;//实际加工工时

            }
        }

        private static void CreateHead(DispatchOrderDoc doc, DispatchOrderAITDTOData dto)
        {
            doc.DocNo = dto.DocNo;
            if (dto.MO == null)
                throw new Exception($"MO不能为空!");
            doc.MO = EntityUtils.GetDocEntity<MO>(dto.MO.Code);
            doc.WorkFlowID = Guid.Empty;
            doc.StateMachineID = Guid.Empty;
            doc.PlanStartDate = doc.MO.StartDate;
            doc.PlanCompleteDate = doc.MO.CompleteDate;
            doc.Org = doc.MO.Org;
            doc.ItemMaster = doc.MO.ItemMaster;
            doc.DispatchQtyDefaultValue = DispatchQtyTypeEnum.OpenQty;
            doc.DispatchOrderDocType = EntityUtils.GetEntityByCode<DispatchOrderDocType>(dto.DispatchOrderDocType);
            doc.BusinessDate = DateTime.Parse(dto.BusinessDate); //单据日期
            doc.WorkCenter=EntityUtils.GetEntityByCode<WorkCenter>(dto.WorkCenter); //工作中心
        }
    }
}
